/**
 * =========================================================================
 *  Copyright 2010 NCS Pte. Ltd. All Rights Reserved
 *
 *  This software is confidential and proprietary to NCS Pte. Ltd. You shall
 *  use this software only in accordance with the terms of the license
 *  agreement you entered into with NCS.  No aspect or part or all of this
 *  software may be reproduced, modified or disclosed without full and
 *  direct written authorisation from NCS.
 *
 *  NCS SUPPLIES THIS SOFTWARE ON AN "AS IS" BASIS. NCS MAKES NO
 *  REPRESENTATIONS OR WARRANTIES, EITHER EXPRESSLY OR IMPLIEDLY, ABOUT THE
 *  SUITABILITY OR NON-INFRINGEMENT OF THE SOFTWARE. NCS SHALL NOT BE LIABLE
 *  FOR ANY LOSSES OR DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING,
 *  MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
 *
 *  Change Revision
 * -------------------------------------------------------------------------
 *  Version    Remarks
 * =========================================================================
 */
package canglang.monitor.core.base.properties;


import org.apache.commons.configuration.CompositeConfiguration;
import org.apache.commons.configuration.ConfigurationFactory;
import org.apache.commons.configuration.SystemConfiguration;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * This class use to get property values from property files
 * 
 * @author: $Author: gracewee $
 * @version: $Rev: 11819 $
 * @modified: $Date: 2009-12-29 19:10:13 +0800 (Tue, 29 Dec 2009) $
 */
public class AppDefaultProperty {

    protected static final Log log = LogFactory.getLog(AppDefaultProperty.class);
    private CompositeConfiguration config;
    private static AppDefaultProperty instance;

    private AppDefaultProperty() {
        this.config = new CompositeConfiguration();
        this.loadConfiguration();
    }

    /**
     * Get the instance of the AppDefaultProperty
     *
     * @return AppDefaultProperty
     */
    public static AppDefaultProperty getInstance() {
        if (instance == null)
            instance = new AppDefaultProperty();
        return instance;
    }

    /**
     * Find the properties field with the key containing special key word
     */
    public static String getProperty(String name) {
        return getInstance().config.getString(name);
    }

    /**
     * Find the properties field with the key containing special key word, the
     * default value be return is no value found.
     */
    public static String getProperty(String name, String defaultValue) {
        if (getInstance().config.containsKey(name)) {
            return getInstance().config.getString(name, defaultValue);
        } else {
            return defaultValue;
        }
    }
    
    /**
     * Loads the properties from the configuration file.
     */
    public void loadConfiguration() {
        SystemConfiguration systemprop = new SystemConfiguration();
        config.addConfiguration(systemprop);
        ConfigurationFactory factory = new ConfigurationFactory();
        try {
            factory.setConfigurationURL(getClass().getClassLoader().getResource("config.xml"));
            config.addConfiguration(factory.getConfiguration());
        } catch (Exception e) {
            log.error("Unable to get configuration file config.xml, it is not a classpath resource.", e);
        }
    }

   
}
